<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100%'
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')

        this.chart.setOption({
          title: {
            text: '今日统计',
            textStyle: {
              color: '#ffffff'
            },
            x: 'left'
          },
          dataset: {
            source: [
              ['score', '数量', 'product'],
              [89.3, 58212, '气瓶'],
              [57.1, 78254, '起重设备'],
              [74.4, 41032, '坠落安全'],
              [50.1, 12755, '交通安全'],
              [89.7, 20145, '化学品安全']
            ]
          },
          grid: { containLabel: true },
          xAxis: {
            name: '数量',
            nameTextStyle: {
              color: '#ffffff'
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#ffffff'
              }
            }
          },
          yAxis: {
            name: '隐患类型',
            nameTextStyle: {
              color: '#ffffff'
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#ffffff'
              }
            },
            type: 'category'
          },
          visualMap: {
            orient: 'horizontal',
            left: 'center',
            min: 10,
            max: 100,
            text: ['高风险', '低风险'],
            textStyle: {
              color: '#ffffff'
            },
            // Map the score column to color
            dimension: 0,
            inRange: {
              color: ['#3cc9c5', '#c357de']
            }
          },
          series: [
            {
              type: 'bar',
              encode: {
                // Map the "amount" column to X axis.
                x: 'amount',
                // Map the "product" column to Y axis
                y: 'product'
              }
            }
          ]
        })
      }
    }
  }
</script>
